/*
 * Author: yezi
 * E-mail: okye328@gmail.com
 * Create time: Apr 20, 2013 12:26:17 AM
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package net.mergebranch.view.resource;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ServletContextAware;

@Controller
@RequestMapping("res")
public class ScriptResourceController implements ServletContextAware {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(ScriptResourceController.class);

	private ServletContext servletContext;

	@Override
	public void setServletContext(ServletContext context) {
		this.servletContext = context;
	}

	public ServletContext getServletContext() {
		return this.servletContext;
	}

	@RequestMapping("css/bootstrap.css")
	public void getBootstrapCss(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String version = ResourceVersion.getBootstrapVer(request
				.getParameter("ver"));

		response.setContentType("text/css");
		InputStream in = getServletContext().getResourceAsStream(
				"WEB-INF/res/css/bootstrap." + version + ".css");
		IOUtils.copy(in, response.getOutputStream());
		// TODO If the agent is Mobile Device, then return the responsive one.
	}

	@RequestMapping("img/glyphicons-halflings.png")
	public void getGlyphicons(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("image/png");
		InputStream in = getServletContext().getResourceAsStream(
				"WEB-INF/res/img/glyphicons-halflings.png");
		IOUtils.copy(in, response.getOutputStream());
	}

	@RequestMapping("img/glyphicons-halflings-white.png")
	public void getGlyphiconsWhite(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("image/png");
		InputStream in = getServletContext().getResourceAsStream(
				"WEB-INF/res/img/glyphicons-halflings-white.png");
		IOUtils.copy(in, response.getOutputStream());
	}

	@RequestMapping("js/bootstrap.js")
	public void getBootstrapJs(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String version = ResourceVersion.getBootstrapVer(request
				.getParameter("ver"));

		response.setContentType("application/x-javascript");
		InputStream in = getServletContext().getResourceAsStream(
				"WEB-INF/res/js/bootstrap." + version + ".js");
		IOUtils.copy(in, response.getOutputStream());
	}

	@RequestMapping("js/jquery.js")
	public void getJquery(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String version = ResourceVersion.getJqueryVer(request
				.getParameter("ver"));

		response.setContentType("application/x-javascript");
		InputStream in = getServletContext().getResourceAsStream(
				"WEB-INF/res/js/jquery-" + version + ".js");
		IOUtils.copy(in, response.getOutputStream());
	}
}
